package com.zrspring.libv2.view.wheel;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import com.zrspring.libv2.R;
import java.util.ArrayList;
import java.util.List;

/**
 * Version: 1.0
 * Author: rui
 * CreateDate: 2017年03月24日11:26 26
 * Encoding:UTF-8
 * Description:
 */
public class CommonOptionPickerView extends LinearLayout {

    private String currentId;
    private int currentPosition;
    private String currentTitle;
    private List<OptionEntity> mOptions;
    private WheelView.OnWheelListener onWheelListener;
    private List<String> wheelStringList;
    private WheelView wheelView;

    public CommonOptionPickerView(Context context) {
        super(context);
        iniWheelView(context);
    }

    public CommonOptionPickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        iniWheelView(context);
    }

    public CommonOptionPickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        iniWheelView(context);
    }

    private void iniWheelView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.wheel_view_common_option, this);
        wheelView = (WheelView) view.findViewById(R.id.wheel_view_common_option_wheelview);
    }


    public String getCurrentId() {
        return this.currentId;
    }

    public int getCurrentPosition() {
        return this.currentPosition;
    }

    public String getCurrentTitle() {
        return this.currentTitle;
    }

    public void cleanAll() {
        if (this.wheelView != null) {
            this.wheelView = null;
        }
        if (this.wheelStringList != null) {
            this.wheelStringList.clear();
            this.wheelStringList = null;
        }
        if (this.mOptions != null) {
            this.mOptions = null;
        }
    }

    public void setOptionsData(List<OptionEntity> paramList) {
        if ((paramList == null) || (paramList.size() == 0)) {
            throw new IllegalArgumentException("options is invalid");
        }
        this.mOptions = paramList;
        if (this.wheelStringList == null) {
            this.wheelStringList = new ArrayList();
        } else {
            this.wheelStringList.clear();
        }
        for (OptionEntity item : paramList) {
            wheelStringList.add(item.getTitle());
        }
        if (this.onWheelListener == null) {
            this.onWheelListener = new WheelView.OnWheelListener() {
                @Override
                public void onSelected(boolean isUserScroll, int index, String item) {
                    OptionEntity localOptionEntity = mOptions.get(index);
                    currentId = localOptionEntity.getId();
                    currentPosition = index;
                    currentTitle = localOptionEntity.getTitle();
                }
            };
        }
        this.wheelView.setItems(wheelStringList);
        this.wheelView.setOnWheelListener(onWheelListener);
    }
}
